<?php
require '../classes/imageClass.php';
require '../classes/albumClass.php';
require'../classes/connectionFactoryClass.php';
require'../classes/imageDao.php';
require'../classes/albumDao.php';
require'../classes/validates.php';
require'checksDate.php';

$album = new AlbumClass;
$image = new ImageClass();
$date = new checksDate();
$c = new ConnectionFactory();
$db = $c->getConnection();
$imageDao = new imageDao($db);
$albumDao = new albumDao($db);
$valid = new validates();



$directory = 'uploads/';
$directoryTmp = '../temp/';

$filePhoto = array();
$filePhotoType = array();
$filePhotoLegend = array();


$filePhotoName = $_POST['fotoname'];
$filePhotoType = $_POST['fototype'];
$filePhotoLegend = $_POST['legend'];
$albumName = $_POST['album'];
$photoCapa = $_POST['capa'];

$album->setNameAlbum($albumName );
$album->setPhotoCapa($photoCapa);
$album->setDirectory($directory);
$album->setDateInclusionAlbum($date->dataAtual());
$result = $albumDao->create($album);

$album->setPhotoCapa($photoCapa);

$re=$albumDao->getIdAlbumByName($photoCapa);

$image->setAlbumId($re);

       
        
        mkdir('../'.$directory.$albumName);
for ($i = 0; $i < count($filePhotoName); $i++) {
    $image->setName_photo($filePhotoName[$i]);
    $image->setType($filePhotoType[$i]);
    $image->setLegend($filePhotoLegend[$i]);


    $tmpFilePath = $directoryTmp . $filePhotoName[$i];
    $image->setDateInclusionPhoto($date->dataAtual());


    if ($tmpFilePath != "") {
        
        $newFilePath = '../'.$directory.$albumName.'/'.$image->getName_photo();
       
        if (copy($tmpFilePath, $newFilePath)) {
            unlink($tmpFilePath);
            $imageDao->create($image);
    }
        else
            echo"<script> alert('O arquivo .$image->getName_photo(). Não foi possível fazer upload'); 
          window.location='../upload.php';</script>";
    } else {
        echo "<script> alert('Selecione um arquivo'); 
          window.location='../upload.php';</script>";
    }
}
?>





<script>



    //<meta http-equiv="content-type" content="text/html;charset=utf-8" />;
    var cadTest = <?php echo $result; ?>;


    if (cadTest) {

        alert("Upload realizado com sucesso!");
        setTimeout("document.location = '../index.php'", 500);

    } else {

        alert("Não Foi Possível Realizar o upload!");
        setTimeout("document.location = '../index.php'", 500);
    }


</script>
